import { parse } from 'url';
// mock tableListDataSource
let tableListDataSource = [];

for (let i = 0; i < 10; i += 1) {
  tableListDataSource.push({
    key: i,
    messageClass: '旅客补偿',
    messageType: '不正常航班',
    flightNo: Math.floor(Math.random()*10000),
    flightDate: new Date(`2018-${Math.floor((Math.random()*10)+2)}-${Math.floor(Math.random()*(31-1+1))+1}`),
    messageContent: '尊敬的旅客，您乘坐的8878航班，代领审核,请快速来取您的物品.',
    number: `136${Math.floor(Math.random()*100000000)}`,
    status: Math.floor(Math.random() * 10) % 2,
    sendPeople: '系统',
    sendTime: new Date(`2019-${Math.floor((Math.random()*10)+2)}-${Math.floor(Math.random()*(31-1+1))+1}`),
  });
}

function getRule(req, res, u) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    // eslint-disable-next-line prefer-destructuring
    url = req.url;
  }

  const params = parse(url, true).query;
  let dataSource = tableListDataSource;

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }

      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(item => {
          if (parseInt(`${item.status}`, 10) === parseInt(s.split('')[0], 10)) {
            return true;
          }

          return false;
        }),
      );
    });
    dataSource = filterDataSource;
  }

  if (params.name) {
    dataSource = dataSource.filter(data => data.name.includes(params.name || ''));
  }

  let pageSize = 10;

  if (params.pageSize) {
    pageSize = parseInt(`${params.pageSize}`, 0);
  }

  const result = {
    data: dataSource,
    total: dataSource.length,
    success: true,
    pageSize,
    current: parseInt(`${params.currentPage}`, 10) || 1,
  };
  return res.json(result);
}

function postRule(req, res, u, b) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    // eslint-disable-next-line prefer-destructuring
    url = req.url;
  }

  const body = (b && b.body) || req.body;
  const { method, name, status, key } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListDataSource = tableListDataSource.filter(item => key.indexOf(item.key) === -1);
      break;

    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        messageClass: '旅客补偿',
        messageType,
        flightNo: Math.floor(Math.random()*1000),
        flightDate: new Date(`2019-06-${Math.floor(i / 2) + 1}`),
        messageContent: '尊敬的旅客，您乘坐的8878航班，代领审核.',
        number: '13312356673',
        status: Math.floor(Math.random() * 10) % 2,
        sendPeople: '系统',
        sendTime: new Date(),
      });

      break;

    case 'update':
      tableListDataSource = tableListDataSource.map(item => {
        if (item.key === key) {
          return { ...item, status, name };
        }

        return item;
      });
      break;

    default:
      break;
  }

  const result = {
    list: tableListDataSource,
    pagination: {
      total: tableListDataSource.length,
    },
  };
  return res.json(result);
}

export default {
  'GET /api/message/messageTable': getRule,
  'POST /api/message/messageTable': postRule,
};
